<?php

namespace App\Imports;


use App\Models\airline\base\FlightModel;
use App\Models\airline\base\RouteModel;
use App\Models\base\CurrencyModel;
use App\Models\CustomerModel;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use stdClass;
use App\Models\DeptModel;
use App\Models\AdminRoleModel;

class PriceImport implements ToCollection, WithMultipleSheets
{


    private $result;

    public function __construct() {
        $this->result = new stdClass;
    }

    public function collection(Collection $rows){
        $lines = [];
        $this->result->error = [];


        foreach ($rows as $k => $row) {
            if($k < 1) continue;

            if($row[0] == ''){
                $this->result->error[] = '第'.$k.'行，客户名称不能为空';
                continue;
            }

            $customer = CustomerModel::where('name', $row[0])->first();

            if(!$customer){
                $this->result->error[] = '第'.$k.'行，客户名称不存在';
                continue;
            }

            if($row[1] == ''){
                $this->result->error[] = '第'.$k.'行，航程不能为空';
                continue;
            }

            $route = RouteModel::where('code', $row[1])->first();

            if(!$route){
                $this->result->error[] = '第'.$k.'行，航程不存在';
                continue;
            }

//            if($row[8] == ''){
//                $this->result->error[] = '第'.$k.'行，航班号不能为空';
//                continue;
//            }
//
//            $flight = FlightModel::where('flight_number', $row[8])->first();
//            if(!$flight){
//                $this->result->error[] = '第'.$k.'行，航班号不存在';
//                continue;
//            }

            if($row[8] == ''){
                $this->result->error[] = '第'.$k.'行，币种不能为空';
                continue;
            }

            $currency = CurrencyModel::where('short_code', $row[8])->first();

            if(!$currency){
                $this->result->error[] = '第'.$k.'行，币种不存在';
                continue;
            }

            $effect_date = is_int($row[9]) ? Date::excelToDateTimeObject($row[9])->format('Y-m-d') : $row[9];

            $lines[] = [
                'customer_id' => $customer->id,
                'route_id' => $route->id,
                'goods_attr' => $row[2] == '普货' ? 1 : 2,
                'currency_id' => $currency->id,
                'effect_date' => $effect_date,
                'price45' => $row[3] ?? 0,
                'price100' => $row[4] ?? 0,
                'price300' => $row[5] ?? 0,
                'price500' => $row[6] ?? 0,
                'price1000' => $row[7] ?? 0,
                'validity' => $row[10] ?? '',
                'remark' => $row[11] ?? '',
            ];
        }

        $this->result->lines = $lines;
    }

    public function sheets(): array {
        return [ 0 => $this];
    }

    public function get_result(){
        return $this->result;
    }


}
